#include "tab3.h"
#include "imgui.h"
#include "mgr.h"
#include <format>
#include <ranges>

using namespace std;

extern bool is_init;
static bool show_is_init = false;
extern ImVec2 btn_size;
static bool show_sign_loc = false;
extern ImVec4 success_color;

void tab3() {
  ImGui::BeginDisabled(!is_init);
  if (ImGui::CollapsingHeader("调试")) {
    debug_info();
  }
  ImGui::EndDisabled();
}

void debug_info() {
  // player
  // target
  // npc
  // grounds
  // 飞行点
  // 任务static boolean show = false;
  static boolean show{false};
  static string msg;
  static char buf[4096]{0};
  if (ImGui::Button("玩家信息", btn_size)) {
    auto player = api()->player();
    if (player) {
      msg +=
          format("{} [0x{:X}]\n[0x{:X}, 0x{:X}]  \n({:.2f}, {:.2f}, {:.2f})\n",
                 player->name(), player->ptr(), player->id1(), player->id2(),
                 player->x(), player->y(), player->z());
    }
    show = true;
    // memset(buf, 0, sizeof(buf));
    // strncpy_s(buf, msg.c_str(), sizeof(buf) - 1);
    // buf[sizeof(buf) - 1] = '\0'; // 确保字符串是以 null 结尾的
  }
  ImGui::SameLine();
  if (ImGui::Button("目标信息", btn_size)) {
    auto p = api()->player();
    auto unitopt = api()->get_unit(p->tid1(), p->tid2());
    if (unitopt.has_value()) {
      auto unit = unitopt.value();
      msg += format("{}[0x{:X}][{:.2f}] [0x{:X}]\n[0x{:X}, 0x{:X}]  "
                    "\n({:.2f}, {:.2f}, {:.2f})\n",
                    unit.name(), unit.creature_type(),
                    p->dist_unit(unit.role()), unit.ptr(), unit.id1(),
                    unit.id2(), unit.x(), unit.y(), unit.z());
    }
    show = true;
  }
  ImGui::SameLine();
  if (ImGui::Button("周围怪物", btn_size)) {
    auto p = api()->player();
    auto units = api()->units();
    for (auto unit : units) {
      msg += format("{}[0x{:X}][{:.2f}] [0x{:X}]\n[0x{:X}, 0x{:X}]  "
                    "\n({:.2f}, {:.2f}, {:.2f})\n",
                    unit->name(), unit->creature_type(),
                    p->dist_unit(unit->role()), unit->ptr(), unit->id1(),
                    unit->id2(), unit->x(), unit->y(), unit->z());
    }
    show = true;
  }
  ImGui::SameLine();
  if (ImGui::Button("周围地面物品", btn_size)) {
    auto grounds = api()->grounds();
    for (auto ground : grounds) {
      msg += format("{}[0x{:X}][{:X}{:X}]  \n({:.2f}, {:.2f}, {:.2f})\n",
                    ground->name(), ground->ptr(), ground->id1(), ground->id2(),
                    ground->x(), ground->y(), ground->z());
    }
    show = true;
  }
  if (ImGui::Button("背包物品", btn_size)) {
    auto items = api()->container_items();
    for (auto item : items) {
      msg += format("{} 稀有度{} 数量{} 价格{} 类型[{:X}{}] 子类型[{:X}{}]\n",
                    item->name(), item->rarity(), item->num(), item->price(),
                    item->type_index(), item->type_name(),
                    item->subtype_index(), item->subtype_name());
    }
    show = true;
  }
  ImGui::SameLine();
  if (ImGui::Button("飞行点遍历", btn_size)) {
    auto size = game()->get_num_taxi_nodes();
    for (auto i : views::iota(0u, size)) {
      auto node_id = game()->get_taxi_node_id(i);
      auto node_name = game()->get_taxi_node_name_ex(node_id);
      msg += format("[0x{:X}]-[{}][0x{:X}]\n", i, node_name, node_id);
    }
    show = true;
  }
  if (ImGui::Button("已接任务", btn_size)) {
    // TODO:
  }
  if (show) {
    if (ImGui::Button("关闭调式信息")) {
      show = false;
      msg.clear();
    }
    ImGui::InputTextMultiline("##copyableText", msg.data(), msg.capacity() + 1,
                              ImVec2(-1, -1), ImGuiInputTextFlags_ReadOnly);
  }
}
